package com.whpost.api.controller.qyh;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.annotation.SaMode;
import cn.dev33.satoken.stp.StpUtil;
import com.whpost.api.controller.BaseController;
import com.whpost.api.utils.ApiUtils;
import com.whpost.service.dao.cwqf.TCwqfKhzfFilesMapper;
import com.whpost.service.dao.cwqf.TFilesMapper;
import com.whpost.service.dao.cwqf.TbCrmCustomerMapper;
import com.whpost.service.entity.cwqf.TFiles;
import com.whpost.service.entity.tools.ResultInfo;
import com.whpost.service.service.cwqf.TCwqfKhzfServices;
import com.whpost.service.service.cwqf.TbCrmCustomerServices;
import com.whpost.service.tools.ToolBean;
import com.whpost.service.tools.Url;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import org.apache.commons.lang3.StringUtils;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileInputStream;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/qyh/khzf")
public class KhzfController {

    @Resource
    private TCwqfKhzfServices tCwqfKhzfServices;
    @Resource
    private TbCrmCustomerServices tbCrmCustomerServices;
    @Resource
    private TFilesMapper tFilesMapper;

    /**客户走访信息查询
     *
     * @param map
     * @return
     */
    @SaCheckLogin
    @RequestMapping(value ="/query")
    public ResultInfo query(@RequestBody Map<String, Object> map){
        String loginId = ApiUtils.getLoginId();
        return tCwqfKhzfServices.query(loginId,map);
    }

    /**
     *  客户走访信息存盘
     * @return
     */
    @RequestMapping(value ="/save")
    public ResultInfo save(@RequestParam(value = "wj", required = false) List<MultipartFile> wj,
                           @RequestParam(value = "data", required = false) String data, HttpServletRequest httpServletRequest){
        System.out.println("List<MultipartFile>:"+wj);
        System.out.println("data:"+data);
        String loginId = ApiUtils.getLoginId();
        return tCwqfKhzfServices.save(loginId,wj,data);
    }

    /** 客户走访删除
     * @param map
     * @return
     */
    @SaCheckLogin
    @RequestMapping(value ="/del")
    public ResultInfo del(@RequestBody Map<String, Object> map){
        String loginId = ApiUtils.getLoginId();
        return tCwqfKhzfServices.del(loginId,map);
    }

    /**客户查询
     * 前端检索使用
     * @param map
     * @return
     */
    @SaCheckLogin
    @RequestMapping(value ="/khcx")
    public ResultInfo khcx(@RequestBody Map<String, Object> map){
        String loginId = ApiUtils.getLoginId();
        return tbCrmCustomerServices.query(loginId,map);
    }

    /**
     * 走访看板
     * @param map
     * @return
     */
    @SaCheckLogin
    @RequestMapping(value ="/zfkb")
    public ResultInfo zfkb(@RequestBody Map<String, Object> map){
        String loginId = ApiUtils.getLoginId();
        return tCwqfKhzfServices.zfkb(loginId,map);
    }

    @RequestMapping(value = "/fj",produces = MediaType.IMAGE_JPEG_VALUE)
    @ResponseBody
    public byte[] fj(String id,String token) {
        try {

            String loginId  = (String)StpUtil.getLoginIdByToken(token);
            if (StringUtils.isEmpty(loginId)) {
                return null;
            }

            TFiles tFiles = tFilesMapper.selectById(id);

            if (tFiles!=null){
                if (!tFiles.getCzyCode().equals(loginId)){
                    return null;
                }else{
                    File file = new File(tFiles.getWjdz()+tFiles.getCfwjm());
                    //ToolBean.info("fj:"+stampCard.getWjljZs());
                    FileInputStream inputStream = null;

                    inputStream = new FileInputStream(file);

                    byte[] bytes = new byte[inputStream.available()];
                    inputStream.read(bytes, 0, inputStream.available());
                    return bytes;
                }

            }else{
                return null;
            }

        } catch (Exception e) {
            return null;
        }
    }
}
